package com.authine.cloudpivot.ext.utils;

/**
 * @author ：zhaozm
 * @date ：Created in 2021/8/4 22:32
 * @description：
 * @modified By：
 * @version: 1.0
 */
import com.alibaba.cola.exception.BizException;
import com.authine.mvp.app.launcher.dto.errorcode.ErrorCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.databind.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

@Slf4j
public class BoUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        String timeZone = StringUtils.isBlank((timeZone = System.getProperty("user.timezone", "Asia/Shanghai"))) ? "Asia/Shanghai" : timeZone;
        objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone));

        //只有不为null的字段才被序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        //如果是空对象的时候,不抛异常
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    public static String toJsonString(Object obj){
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static <T> List<T> copy2List(Object source, Class<T> clazz){
        try {
            TypeFactory typeFactory = objectMapper.getTypeFactory();
            return objectMapper.readValue(toJsonString(source), typeFactory.constructCollectionLikeType(List.class, clazz));
            //return objectMapper.readValue(toJsonString(source), new TypeReference<List<T>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static <T> T Map2Bo(Map<String, Object> map, Class<T> clazz){
        T t;
        try {
            t = objectMapper.readValue(objectMapper.writeValueAsString(map), clazz);
        } catch (JsonProcessingException e) {
            throw bizException(e.getMessage());
        }
        return t;
    }

    public static Map<String, Object> bo2Map(Object obj){
        Map<String, Object> map;
        try {
            map = objectMapper.readValue(objectMapper.writeValueAsString(obj), new TypeReference<Map<String, Object>>() {});
        } catch (JsonProcessingException e) {
            throw bizException(e.getMessage());
        }
        return map;
    }

    public static BizException bizException(String errorMessage){
        ErrorCodeEnum bizError = ErrorCodeEnum.BIZ_EXCEPTION;
        bizError.setErrDesc(errorMessage);
        return new BizException(bizError,errorMessage);
    }
}
